<template>
  <div class="search">
     <div class="header">
        <div class="icon" @click="goBack">
          <span class="iconfont icon-arrow-left"></span>
        </div>
        <div class="title">
          搜索
        </div>
    </div>

    <div class="searchTop">
       <input type="text" class="keyword" v-model="keyword" @change="clickSearch">
       <div class="searchBton" @click="clickSearch">
        <span class="iconfont icon-search"></span>
        <input type="button" value="搜索" class="btn">
       </div>
    </div>

    <div class="histroy" v-show="hisArr.length">
      <div class="top">
         <div class="title">历史记录</div>
         <div class="icon" @click="onDel">
         <span class="iconfont icon-ashbin"></span>
      </div>
      </div>
      <div class="list">
          <div class="item" v-for="item in hisArr" :key="item" @click="histroyItem(item)">{{item}}</div>
      </div>
    </div>

    <div class="seaMain" v-show="listArr.length">
      <div class="row" v-for="item in listArr" :key="item.id" @click="contItem({id:item.id,cid:item.classid})">{{item.title}}</div>
    </div>

    <div class="noData" v-show="!listArr.length">
      <div>
        <span class="iconfont icon-warning"></span>
      </div>
      <div class="text">
        暂无内容
      </div>
    </div>
  </div>
</template>

<script>
import { searchFun } from '@/network/innerAxios'
export default {
  data () {
    return {
      keyword: '',
      hisArr: [],
      listArr: []
    }
  },
  mounted () {
    let locaArr = window.localStorage.getItem('locaArr')
    locaArr = JSON.parse(locaArr)
    if (!locaArr) {
      this.hisArr = []
      return
    }
    this.hisArr = locaArr
  },
  methods: {
    // 点击内容每一项
    contItem (obj) {
      this.$router.push({ path: '/detail', query: obj })
    },
    // 历史每一项
    histroyItem (item) {
      this.keyword = item
      this.clickSearch()
    },
    // 删除缓存
    onDel () {
      window.localStorage.removeItem('locaArr')
      this.hisArr = []
    },
    // 返回
    goBack () {
      this.$router.back()
    },
    // 搜索
    clickSearch () {
      this.hisArr.unshift(this.keyword)
      // 去重功能
      this.hisArr = [...new Set(this.hisArr)]
      window.localStorage.setItem('locaArr', JSON.stringify(this.hisArr))
      //  获取网络请求
      searchFun(this.keyword).then(res => {
        this.listArr = res.data
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search{
  .header{
    display: flex;
    justify-content: space-between;
    height: 1rem;
    color: #fff;
    background-color: #31C27C;
    align-items: center;
    text-align: center;
    padding-right: 0.7rem;
    .icon{
      flex: 1;
      span{
        font-size: 0.6rem;
        display: block;
      }
    }
    .title{
      flex: 5;
      font-size: 0.4rem;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      padding-right: 0.4rem;
      padding-left: 0.6rem;
    }
  }
  .searchTop{
    height: 0.9rem;
    background-color: #31C27C;
    border-top: 1px solid #efefef;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 0.2rem;
    input{
      outline: none;
    }
    .keyword{
      height: 0.6rem;
      border: none;
      border-radius: 0.06rem;
      width: 80%;
      padding-left: 0.1rem;
      color: #555;
    }
    .searchBton{
      display: flex;
      justify-content: space-around;
      width:20%
    }
    span{
      font-size: 0.6rem;
      color: #fff;
    }
    .btn{
      border:none;
      height:0.6rem;
      width: 100%;
      border-radius: 0.06rem;
      background-color: #fff;
    }
  }
  .histroy{
    padding: 0.3rem 0.2rem;
    .top{
     display: flex;
     justify-content: space-between;
     align-items: center;
     .title{
       font-size: 0.36rem;
     }
     .icon{
       .iconfont{
         display: block;
         font-size: 0.5rem;
       }
     }
    }
    .list{
      display: flex;
      padding: 0.2 0rem;
      flex-wrap: wrap;
      .item{
        background-color: #efefef;
        color: #333;
        padding: 0.1rem 0.2rem;
        font-size: 0.28rem;
        margin-bottom: 0.2rem;
        margin-right: 0.2rem;
        border-radius: 10rem;
      }
    }
  }
  .seaMain{
    padding: 0.2rem;
    background-color: #f5f5f5;
    width: 7.1rem;
    margin: 0 auto;
    .row{
      font-size: 0.28rem;
      color: #333;
      line-height: 2em;
      padding-left: 0.3rem;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      position: relative;
      &::before{
        position: absolute;
        content: '';
        top: 0;
        left: 0;
        top:calc(50% - 0.04rem);
        background: #31C27C;
        width: 0.08rem;
        height: 0.08rem;
        border-radius: 50%;
        box-shadow: 0 0 0.1rem rgba(49, 194, 124, 0.5);
      }
    }
  }
  .noData{
    width: 7.1rem;
    margin: 0 auto;
    text-align: center;
    font-size: 0.44rem;
    padding: 1rem 0.2rem;
    color: #888;
    .iconfont{
      font-size: 2rem;
    }
  }
}
</style>
